<template>
  <div>
    <el-sub-menu ref="subMenu" :index="resolvePath(item.id)" popper-append-to-body>
      <template #title>
        <el-link :href="'/iots/area-devices/'+item.id" :underline="false">
          <svg-icon icon-class="tree"/>
          <span class="menu-title">{{ item.display_name }}</span>
        </el-link>
      </template>
      <sidebar-item
          v-for="child in item.children"
          :key="child.id"
          :is-nest="true"
          :item="child"
          :base-path="resolvePath(child.id)"
          class="nest-menu"
      />
    </el-sub-menu>
  </div>
</template>

<script setup name="sidebar-index">

import {isExternal} from '@/utils/validate'
import AppLink from './Link'
import {getNormalPath} from '@/utils/ruoyi'
import {ref} from "vue";

const props = defineProps({
  // route object
  item: {
    type: Object,
    required: true
  },
  isNest: {
    type: Boolean,
    default: false
  },
  basePath: {
    type: String,
    default: ''
  }
})

const onlyOneChild = ref({});

function hasOneShowingChild(children = [], parent) {
  if (!children) {
    children = [];
  }
  const showingChildren = children.filter(item => {
    if (item.hidden) {
      return false
    } else {
      onlyOneChild.value = item
      return true
    }
  })

  // When there is only one child router, the child router is displayed by default
  if (showingChildren.length === 1) {
    return true
  }

  // Show parent if there are no child router to display
  if (showingChildren.length === 0) {
    onlyOneChild.value = {...parent, path: '', noShowingChildren: true}
    return true
  }

  return false
}

function resolvePath(areaId) {
  return `/area-devices/${areaId}`
}

</script>
